using System;
using System.IO;
using FireStarterModeller.BO;
using Habanero.Base;

namespace FireStarter.CodeGeneration
{
    public class VWGProjectGenerator : ProjectGenerator
    {
        public override Result Generate()
        {
            CheckRequiredPropertiesSet();
            //if (ModelSolution.LogicProjectName == null) throw new PropertyMissingException("LogicProjectName");
            if (ModelSolution.BOProjectName == null) throw new PropertyMissingException("BOProjectName");
            if (ModelSolution.UIVWGProjectName == null) throw new PropertyMissingException("UIVWGProjectName");
            
            Directory.CreateDirectory(ProjectPath);

            VWGProjectTemplateLoader vwgProjectTemplateLoader = new VWGProjectTemplateLoader();
            string projectFileName = Path.Combine(ProjectPath, ProjectName + ".csproj");
            ProjectFileManager projectFileManager = new ProjectFileManager(projectFileName, OutputType.Library, true, TemplateLoader.CsprojVWG(SolutionGenerator.CodeTemplate), SolutionGenerator.CodeTemplate);
            string contents = "";
            if (String.IsNullOrEmpty(ModelSolution.UIProjectName))
            {
                contents = vwgProjectTemplateLoader.GetMainFormForVWGOnly(ProjectName, ProjectName, null, ModelSolution.ApplicationName,
                                                                  ModelSolution.UIVWGProjectName);
                string programFileName = Utilities.CreateFile(ProjectPath, "MainForm.cs", contents);
                projectFileManager.AddCodeFile(programFileName);
            }
            else
            {
                contents = vwgProjectTemplateLoader.GetUiManagerVWG(ProjectName, ModelSolution.UIProjectName,
                    ModelSolution.UIVWGProjectName, ModelSolution.ApplicationName);
                string uiManagerFileName = Utilities.CreateFile(ProjectPath, "UIManagerVWG.cs", contents);
                projectFileManager.AddCodeFile(uiManagerFileName);

                contents = vwgProjectTemplateLoader.GetMainForm(ProjectName);
                string mainFormFileName = Utilities.CreateFile(ProjectPath, "MainForm.cs", contents);
                projectFileManager.AddCodeFile(mainFormFileName);
            }

            contents = vwgProjectTemplateLoader.GetGlobal_asax(ProjectName);
            string globalAsaxFileName = Utilities.CreateFile(ProjectPath, "Global.asax", contents);

            contents = vwgProjectTemplateLoader.GetGlobal_asax_cs(ProjectName, ProjectName, null,
                ModelSolution.ApplicationName, ModelSolution.BOProjectName);
            string globalAsaxCsFileName = Utilities.CreateFile(ProjectPath, "Global.asax.cs", contents);
     
            contents = vwgProjectTemplateLoader.GetWebConfig((DMSolution) SolutionGenerator.ModelSolution);
            string webConfigFileName = Utilities.CreateFile(ProjectPath, "Web.config", contents);

            contents = vwgProjectTemplateLoader.GetAssemblyInfo(ProjectName);
            string assemblyInfoFileName = Utilities.CreateFile(ProjectPath, "Properties\\AssemblyInfo.cs", contents);
            
            
            
            contents = vwgProjectTemplateLoader.GetVWGCSProjUser();
            Utilities.CreateFile(ProjectPath, ProjectName + ".csproj.user", contents);

            projectFileManager.AddContentFile(globalAsaxFileName);
            projectFileManager.AddCodeFile(globalAsaxCsFileName, globalAsaxFileName);
            projectFileManager.AddFile(webConfigFileName);
            projectFileManager.AddProjectReference(ModelSolution.UIProjectName, SolutionGenerator.SolutionDirectory);
            projectFileManager.AddProjectReference(ModelSolution.BOProjectName, SolutionGenerator.SolutionDirectory);
            projectFileManager.AddProjectReference(ModelSolution.LogicProjectName, SolutionGenerator.SolutionDirectory);
            projectFileManager.AddProjectReference(ModelSolution.UIVWGProjectName, SolutionGenerator.SolutionDirectory);

            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "Habanero.Base.dll"));
            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "Habanero.BO.dll"));
            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "Habanero.DB.dll"));
            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "Habanero.Faces.Base.dll"));
            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "Habanero.Faces.VWG.dll"));
            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "log4net.dll"));
            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "Gizmox.WebGUI.Forms.dll"));
            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "Gizmox.WebGUI.Common.dll"));
            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "Gizmox.WebGUI.Server.dll"));
            projectFileManager.AddSystemReference("System.Web");

            projectFileManager.SaveProjectFile();
            return new Result(true);
        }
    }
}